package com.itheima.wms.controller;

import com.itheima.wms.pojo.GoodsType;
import com.itheima.wms.pojo.GoodsTypeDto;
import com.itheima.wms.service.GoodsTypeService;
import com.itheima.wms.utils.PageBean;
import com.itheima.wms.utils.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/goodsType")
@RequiredArgsConstructor
public class GoodsTypeController {

    private final GoodsTypeService goodsTypeService;

    @GetMapping("/page")
    public Result goodsTypePage(GoodsTypeDto goodsTypeDto) {
        PageBean<GoodsType> goodsTypePageBean = goodsTypeService.goodsTypePage(goodsTypeDto);
        return Result.success(goodsTypePageBean);
    }

    @PostMapping
    public Result insertGoodsType(@RequestBody GoodsType goodsType) {
        goodsTypeService.insert(goodsType);
        return Result.success("插入成功");
    }

    @GetMapping(path = "/{id}")
    public Result getGoodsTypeById(@PathVariable Long id) {
        GoodsType goodsType = goodsTypeService.getgoodsTypeById(id);
        return Result.success(goodsType);
    }

    @PutMapping
    public Result updateGoodsType(@RequestBody GoodsType goodsType) {
        goodsTypeService.update(goodsType);
        return Result.success("修改成功");
    }

    @DeleteMapping
    public Result deleteGoodsType(@RequestParam("ids[]") List<Long> ids) {
        log.info("删除的编号为{}", ids);
        Long id = ids.get(0);
        goodsTypeService.delete(id);
        return Result.success("删除成功");
    }

    //模糊查询商品类型
    @GetMapping("/vague")
    public Result vagueQueryGoodsType(GoodsTypeDto goodsTypeDto) {
        List<GoodsType> goodsTypeList = goodsTypeService.vagueQueryGoodsType(goodsTypeDto);
        return Result.success(goodsTypeList);
    }

}
